package yuejian_07.yuejian_07.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.Toast;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.activity.ActSearchActivity;
import yuejian_07.yuejian_07.activity.ConSearchActivity;
import yuejian_07.yuejian_07.activity.MasSearchActivity;

public class HomeFragment extends Fragment  {
    private View view=null;
    private ImageView search;//搜索
    private RollPagerView rollPV = null;

    private HomeJingxuanFragment activityFragment;
    private HomeJinqiFragment contestFragment;
    private HomeQuanweiFragment masterFragment;

    private RadioButton activityRadio;
    private RadioButton masterRadio;
    private RadioButton contestRadio;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    public HomeFragment(){}

    PopupWindow popup;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view= inflater.inflate(R.layout.tab03_home, container, false);
        initViews();
        initEvents();
        initPopupWindow();
        return view;
    }

    /***********控件初始化***********/
    public void initViews(){
        search=(ImageView)view.findViewById(R.id.search);
        rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
    }

    /***********相关事件实现***********/
    public void initEvents(){

        //点击搜索按钮弹出活动，比赛，大师选项
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                search.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        /*相对于某个控件的下方，并且在View这个布局中能找到对应的控件，如果时include的就不能找见*/
                        popup.showAsDropDown(view.findViewById(R.id.search), 0,
                                0);
                    }
                });

            }
        });

        /****************轮播图*****************/
        rollPV.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPV.setAnimationDurtion(500);
        //设置指示器:
        //rollPV.setHintView(new IconHintView());
        //rollPV.setHintView(new IconHintView(this,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        rollPV.setHintView(new ColorPointHintView(getActivity(), getResources().getColor(R.color.colorPrimary),
                Color.WHITE));
        //设置适配器
        rollPV.setAdapter(new RollPagerAdapter());

        //设置每一个图片的点击事件
        /*rollPV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(), "点击了图片"+position, Toast.LENGTH_SHORT).show();

            }
        });*/
    }

    //轮播图相关
    private int images[] = {
            R.drawable.lunbo1,
            R.drawable.lunbo2,
            R.drawable.lunbo3
    };
    //轮播图相关
    private class RollPagerAdapter extends StaticPagerAdapter {

        @Override
        public View getView(ViewGroup container, int position) {

            ImageView view=new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return view;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    }

    //搜索弹窗相关
    private void initPopupWindow() {
        View v = getActivity().getLayoutInflater().inflate(
                R.layout.pop_search, null);


        popup = new PopupWindow(v,200, 330, true);


        popup.setFocusable(true);
        //该属性设置为true则你在点击屏幕的空白位置也会退出
        popup.setTouchable(true);

        popup.setOutsideTouchable(true);
        popup.setAnimationStyle(R.style.PopupAnimation);
        popup.setBackgroundDrawable(new ColorDrawable(0x8f000000));
        handleLogic(v);
    }

    /** 处理弹出显示内容、点击事件等逻辑*/
    private void handleLogic(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popup != null) {
                    popup.dismiss();
                }
                String showContent = "";
                switch (v.getId()) {
                    case R.id.activity_btn:
                        showContent = "活 动";
                        break;
                    case R.id.contest_btn:
                        showContent = "比 赛";
                        break;
                    case R.id.master_btn:
                        showContent = "大 师";
                        break;
                }
                Toast.makeText(HomeFragment.this.getActivity(), showContent, Toast.LENGTH_SHORT).show();
            }
        };
        contentView.findViewById(R.id.activity_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActSearchActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        contentView.findViewById(R.id.contest_btn).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),ConSearchActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        contentView.findViewById(R.id.master_btn).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),MasSearchActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }


    //精选活动，近期比赛，权威大师等fragment
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        activityRadio = (RadioButton) view.findViewById(R.id.jingxuanActivity);
        masterRadio = (RadioButton) view.findViewById(R.id.quanweiMaster);
        contestRadio = (RadioButton) view.findViewById(R.id.jinqiContest);

        activityRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    activityFragment = new HomeJingxuanFragment();
                    replaceFragment(activityFragment);
                }
            }
        });

        contestRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    contestFragment = new HomeJinqiFragment();
                    replaceFragment(contestFragment);
                }
            }
        });

        masterRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    masterFragment = new HomeQuanweiFragment();
                    replaceFragment(masterFragment);
                }
            }
        });


        fragmentManager=getChildFragmentManager();
        activityRadio.setChecked(true);

    }
    private void replaceFragment(Fragment fragment){
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }



}